package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.query.DestinationQueryObject;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by admin on 2019/8/9.
 */
@Controller
@RequestMapping("/destination")
public class DestinationControler {

    @Reference
    private IDestinationService destinationService;

    // 查询目的地列表
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") DestinationQueryObject qo) {
        model.addAttribute("pageInfo", destinationService.query(qo));
        model.addAttribute("toasts", destinationService.queryForParent(qo.getParentId()));
        return "/destination/list";
    }

    // 修改目的地热门状态
    @GetMapping("/changeHotValue")
    @ResponseBody
    public AjaxResult changeHotValue(int hot, Long id) {
        try {
            destinationService.changeHotValueById(hot, id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "修改失败");
        }
        return new AjaxResult(true);
    }

    // 删除目的地
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id) {

        try {
            destinationService.deleteDestinationById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
        return new AjaxResult(true);
    }

    // 修改目的地内容
    @RequestMapping("/setInfo")
    @ResponseBody
    public AjaxResult setInfo(Long id, String info) {

        try {
            destinationService.setInfo(id, info);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "保存失败");
        }
        return new AjaxResult(true);
    }
}
